part of 'component.dart';

class CodeField extends StatefulWidget {
  const CodeField({Key? key, required this.controller}) : super(key: key);
  final TextEditingController controller;
  @override
  _CodeFieldState createState() => _CodeFieldState();
}

class _CodeFieldState extends State<CodeField> {
  String actionText = '获取验证码';
  int timeCount = 60;
  Timer? timer;
  bool isSending = false;
  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
    timer?.cancel();
  }

  void sendAction() {
    if (isSending) {
      return;
    }
    isSending = true;
    timer = Timer.periodic(const Duration(seconds: 1), (_) {
      if (timeCount == 0) {
        isSending = false;
        timer?.cancel();
        timeCount = 60;
        actionText = '获取验证码';
      } else {
        timeCount--;
        actionText = '${timeCount}s';
      }
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 50.w,
      alignment: Alignment.center,
      child: TextField(
        maxLength: 10,
        keyboardType: TextInputType.number,
        textAlignVertical: TextAlignVertical.center,
        decoration: InputDecoration(
          isCollapsed: true,
          counterText: '',
          suffixIcon: InkWell(
              onTap: sendAction,
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Text(
                    actionText,
                    style: pfMediumStyle(
                        color:
                            isSending ? ColorsUtil.content : ColorsUtil.green,
                        fontSize: font14),
                  ),
                ],
              )),
          hintText: '请输入短信验证码',
          hintStyle:
              pfMediumStyle(color: ColorsUtil.placeholder, fontSize: font15),
          enabledBorder: const UnderlineInputBorder(
              borderSide: BorderSide(color: ColorsUtil.separator),
              borderRadius: BorderRadius.zero),
          disabledBorder: const UnderlineInputBorder(
              borderSide: BorderSide(color: ColorsUtil.separator),
              borderRadius: BorderRadius.zero),
          focusedBorder: const UnderlineInputBorder(
              borderSide: BorderSide(color: ColorsUtil.separator),
              borderRadius: BorderRadius.zero),
        ),
      ),
    );
  }
}
